﻿Imports KFZ_Ausgaben.clsFunctions

Public Class frmNewFuelEntry
    Private m_sCurrentGasStation As String
    Private m_pListOfGasStations As List(Of String)

    Private ReadOnly Property DeniedColor As Color
        Get
            Return Color.LightCoral
        End Get
    End Property

    Private ReadOnly Property FuelDate As Date
        Get
            Return pDateTimePicker.Value
        End Get
    End Property
    Private ReadOnly Property Price As String
        Get
            If txtPrice.Text = String.Empty Then
                Return Nothing
            Else
                Return txtPrice.Text '.Replace(",", ".")
            End If
        End Get
    End Property
    Private ReadOnly Property Liter As String
        Get
            If txtLiter.Text = String.Empty Then
                Return Nothing
            Else
                Return txtLiter.Text '.Replace(",", ".")
            End If
        End Get
    End Property
    Private ReadOnly Property PricePerLiter As String
        Get
            If txtPricePerLiter.Text = String.Empty Then
                Return Nothing
            Else
                Return txtPricePerLiter.Text '.Replace(",", ".")
            End If
        End Get
    End Property
    Private ReadOnly Property GasStation As String
        Get
            Return cbGasStation.Text
        End Get
    End Property
    Private ReadOnly Property Kilometer As String
        Get
            If txtKilometer.Text = String.Empty Then
                Return "0,0"
            Else
                Return txtKilometer.Text '.Replace(",", ".")
            End If
        End Get
    End Property

    Public ReadOnly Property RefuelingEntry As clsRefueling
        Get
            Dim pEntry As New clsRefueling

            pEntry.RefuelDate = Me.FuelDate
            pEntry.Price = Me.Price
            pEntry.Liter = Me.Liter
            pEntry.PricePerLiter = Me.PricePerLiter
            pEntry.GasStation = Me.GasStation
            pEntry.LastDistance = Me.Kilometer

            Return pEntry
        End Get
    End Property

    Public Sub SetEntryToEdit(ByVal pEntry As clsRefueling)
        Me.Text = "Eintrag bearbeiten: Tanken"

        pDateTimePicker.Value = pEntry.RefuelDate
        txtPrice.Text = FormatDecimal(pEntry.Price)
        txtLiter.Text = FormatDecimal(pEntry.Liter)
        txtPricePerLiter.Text = FormatDecimal(pEntry.PricePerLiter, 3)
        cbGasStation.Text = pEntry.GasStation
        txtKilometer.Text = FormatDecimal(pEntry.LastDistance)
    End Sub

    Public Sub CreateGasStationList(ByVal pRefuelings As List(Of clsRefueling))
        cbGasStation.Sorted = True

        m_pListOfGasStations = New List(Of String)

        For Each pEntry In pRefuelings
            If Not m_pListOfGasStations.Contains(pEntry.GasStation) Then
                m_pListOfGasStations.Add(pEntry.GasStation)
            End If
        Next

        FillGasStationComboBox("")
    End Sub

    Private Sub FillGasStationComboBox(ByVal sFilterString As String)
        cbGasStation.Items.Clear()

        For Each pEntry In m_pListOfGasStations
            If pEntry.ToLower.StartsWith(sFilterString.ToLower) Then
                cbGasStation.Items.Add(pEntry)
            End If

        Next
    End Sub

    Private Function GetFirstMatch(ByVal sFilterString As String) As String
        If sFilterString.Length = 0 Then Return sFilterString

        For Each pEntry In m_pListOfGasStations
            If pEntry.ToLower.StartsWith(sFilterString.ToLower) Then Return pEntry
        Next

        Return sFilterString
    End Function

    Private Sub AutoCompletePrices()
        If txtPrice.Text <> String.Empty And _
            txtLiter.Text <> String.Empty And _
            txtPricePerLiter.Text <> String.Empty Then
            Return
        End If

        If Price IsNot Nothing And Liter IsNot Nothing Then
            txtPricePerLiter.Text = FormatDecimal(Math.Round(Price / Liter, 3), 3)
        ElseIf Price IsNot Nothing And PricePerLiter IsNot Nothing Then
            txtLiter.Text = FormatDecimal(Math.Round(Price / PricePerLiter, 2))
        ElseIf Liter IsNot Nothing And PricePerLiter IsNot Nothing Then
            txtPrice.Text = FormatDecimal(Math.Round(Liter * PricePerLiter, 2))
        End If
    End Sub

    Private Function IsValid()
        If Price Is Nothing Then
            txtPrice.BackColor = DeniedColor
            Return False
        End If
        If Liter Is Nothing Then
            txtLiter.BackColor = DeniedColor
            Return False
        End If
        If PricePerLiter Is Nothing Then
            txtPricePerLiter.BackColor = DeniedColor
            Return False
        End If

        If Not (PricePerLiter = Math.Round(Price / Liter, 3)) Then
            txtPrice.BackColor = DeniedColor
            txtLiter.BackColor = DeniedColor
            txtPricePerLiter.BackColor = DeniedColor
            Return False
        End If

        Return True
    End Function

    Private Sub btnAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAccept.Click
        If IsValid() Then
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Else
            MsgBox("Daten sind ungültig. Bitte überprüfen Sie," + vbCrLf + "ob Sie alle Pflichtfelder (*) ausgefüllt haben und alle Daten korrekt sind.", MsgBoxStyle.Exclamation+MsgBoxStyle.OkOnly,"Daten ungültig")
        End If
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub frmNewFuelEntry_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If cbGasStation.Focused Then
            e.Handled = True
            e.SuppressKeyPress = True

            If e.KeyValue >= &H41 And e.KeyValue <= &H5A Then
                If e.Shift Then
                    m_sCurrentGasStation += ChrW(e.KeyValue)
                Else
                    m_sCurrentGasStation += ChrW(e.KeyValue + &H20)
                End If

            ElseIf e.KeyData = Keys.Back Then
                If m_sCurrentGasStation.Length > 0 Then
                    m_sCurrentGasStation = m_sCurrentGasStation.Substring(0, m_sCurrentGasStation.Length - 1)
                End If

            End If

            cbGasStation.Text = GetFirstMatch(m_sCurrentGasStation)


            cbGasStation.Select(m_sCurrentGasStation.Length, cbGasStation.Text.Length - m_sCurrentGasStation.Length)

        End If
    End Sub

    Private Sub frmNewFuelEntry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_sCurrentGasStation = ""
        txtPrice.Select()
    End Sub

    Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLiter.GotFocus, txtPrice.GotFocus, txtPricePerLiter.GotFocus
        CType(sender, TextBox).SelectAll()
    End Sub

    Private Sub TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLiter.LostFocus, txtPrice.LostFocus, txtPricePerLiter.LostFocus
        AutoCompletePrices()
        txtPrice.BackColor = System.Drawing.SystemColors.Window
        txtLiter.BackColor = System.Drawing.SystemColors.Window
        txtPricePerLiter.BackColor = System.Drawing.SystemColors.Window
    End Sub

    Private Sub cbGasStation_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbGasStation.LostFocus
        m_sCurrentGasStation = ""
    End Sub
End Class